package apresentacao.caminhoneiro;


import dao.caminhoneiro.AsyncTask_Buscar_Fretes_Estado;
import tcc.caminhoneiro.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Fragment_Buscar_Fretes_Estado extends Fragment{
	Spinner  spEstadoOrigem, spEstadoDestino;
	Button btBuscar;
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_buscar_fretes_estado, null);
		inicializar(view);
		return view;
	}

	
	OnClickListener eventoBuscar = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AsyncTask_Buscar_Fretes_Estado async = new AsyncTask_Buscar_Fretes_Estado(spEstadoOrigem.getSelectedItem().toString(), spEstadoDestino.getSelectedItem().toString(), getActivity());
			async.execute();
		}
	};
	
	private void inicializar(View view) {
		spEstadoOrigem 	= (Spinner) view.findViewById(R.id.buscar_fretes_estado_spEstadoOrigem);
		spEstadoDestino = (Spinner) view.findViewById(R.id.buscar_fretes_estado_spEstadoDestino);
		btBuscar 		= (Button) view.findViewById(R.id.buscar_fretes_btBuscar);
		
		initEstadoDestino();
		initEstadoOrigem();
		btBuscar.setOnClickListener(eventoBuscar);
		
		OnItemSelectedListener eventoEstadoOrigem = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		spEstadoOrigem.setOnItemSelectedListener(eventoEstadoOrigem);
		
		OnItemSelectedListener eventoEstadoDestino = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		spEstadoDestino.setOnItemSelectedListener(eventoEstadoDestino);		
	}
	
	public void initEstadoOrigem(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.estados, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spEstadoOrigem.setAdapter(adapter);
	}
	
	public void initEstadoDestino(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.estados, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spEstadoDestino.setAdapter(adapter);
	}
}
